package com.isoft.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.bean.MyMailMessage;
import com.isoft.dao.TicketsDao;
import com.isoft.entity.FlightLine;
import com.isoft.entity.PageBean;
import com.isoft.entity.Tickets;
import com.isoft.entity.User;
import com.isoft.service.ITicketsService;
import com.isoft.util.MyMailUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TicketService implements ITicketsService {
    @Autowired
    TicketsDao ticketsDao;

    @Autowired
    MyMailUtil myMailUtil;

    @Autowired
    TemplateEngine templateEngine;

    @Override
    public Boolean addTickets(Tickets tickets){
        ticketsDao.addTickets(tickets);
        Context context = new Context();
        Map<String, Object> map = new HashMap<>();
        map.put("ticket",tickets);
        context.setVariables(map);
        String emailText = templateEngine.process("bookTicket.html", context);
        MyMailMessage message = new MyMailMessage();
        message.setTo("3284723323@qq.com");
        message.setSubject("购票成功通知");
        message.setText(emailText);
        myMailUtil.sendHtmlMail(message);
        return ticketsDao.addTickets(tickets)>0;
    }

    @Override
    public IPage<Tickets> selectByUserid(Integer currentPage, Integer size, Integer userid) {
        Page<Tickets> pageRequest = new Page<>(currentPage, size);
        IPage<Tickets> ticketPage = ticketsDao.selectByUserid(pageRequest,userid);
        return ticketPage;
    }

    @Override
    public Boolean tuipiao(Integer id) {
        return ticketsDao.tuipiao(id)>0;
    }

    @Override
    public IPage<Tickets> showTickets(Integer currentPage, Integer size){
        Page<Tickets> pageRequest = new Page<>(currentPage, size);
        IPage<Tickets> ticketPage = ticketsDao.showTickets(pageRequest);
        return ticketPage;
    }
}
